<?php
class ControllerSaleThongKeTheoThang extends Controller {
	private $error = array();

	public function index() {
	$this->language->load('sale/customer');
		$this->data['token'] = $this->session->data['token'];
		$this->document->setTitle('Thống kê theo tháng');
		$this->template = 'sale/thong_ke_theo_thang.tpl';	
		$this->children = array(
			'common/footer',
			'common/header'	
		);
		$this->response->setOutput($this->render());	
	}
	public function getList() {
		$this->language->load('sale/customer');
		$this->load->model('sale/customer');
		$data = array(
			'filter_date_start' => $this->request->get['filter_date_start'],
			'filter_date_end'   => $this->request->get['filter_date_end'],
			'status'            => 'true'
		);
		$this->data['customers'] = array();
		// hoa hồng trực tiếp
		$results1 = $this->model_sale_customer->getTotalHHTrucTiepByDate($data);	
		// hoa hồng gián tiếp
	
		$results2 = $this->model_sale_customer->getTotalHHGianTiepByDate($data);
		
		if($results2){
			foreach($results2 as $key => $result){
				$results1[$key]['giantiep'] = $result['giantiep'];
			}
		}
		foreach($results1 as $key => $value){
			$customer_info = $this->model_sale_customer->getCustomer($key);
			if($customer_info){
				$this->data['customers'][] = array(
				'customer_id'    => $customer_info['customer_id'],
				'madn'           => $customer_info['madn'],
				'telephone'      => $customer_info['telephone'],
				'name'           => $customer_info['name'],
				'email'          => $customer_info['email'],
				'bank'           => $customer_info['bank'],
				'tructiep'       => (isset($value['tructiep'])) ? $this->currency->format($value['tructiep']) : 0,
				'giantiep'       => (isset($value['giantiep'])) ? $this->currency->format($value['giantiep']) : 0
				);
			}
		}
		$this->template = 'sale/form_thong_ke_theo_thang.tpl';

		$this->response->setOutput($this->render());
	}
	
	public function activeTransaction() {
		$this->language->load('sale/customer');
		$this->load->model('sale/customer');
		$results = $this->model_sale_customer->getCustomer($this->request->get['customer_id']);
		if($results){
			$data = array(
				'customer_id'       => $this->request->get['customer_id'],
				'filter_date_start' => $this->request->get['filter_date_start'],
				'filter_date_end'   => $this->request->get['filter_date_end']
			);
			// hoa hồng trực tiếp
			$this->model_sale_customer->transactionsHHTrucTiep($data);	
			// hoa hồng gián tiếp
			$this->model_sale_customer->transactionsHHGianTiep($data);
			
			$this->model_sale_customer->addTransactionsCustomer($this->request->get['customer_id'], $this->request->get['total'], $this->request->get['comment']);
			
			$this->session->data['success'] = 'Hệ thống thanh toán thành công ' . $this->currency->format($this->request->get['total']) . ' cho thành viên ' . $results['madn'];
		} else {
			$this->session->data['warning'] = 'Vui long kiểm tra lại, thành viên không tồn tại trong hệ thống';
		}
		$this->redirect($this->url->link('sale/customer', 'token=' . $this->session->data['token'], 'SSL'));
	}
}
?>